<template>
  <div>
    <tablePaging :tableItem="tableItem" :operationItem="operationItem" />
  </div>
</template>

<script setup>
import tablePaging from '@/views/common/component/table-paging'
import { onMounted, reactive, ref } from 'vue'
import {
  tableItem,
  operationItem,
} from './const.js'
import {publishFriendlyLink, delFriendlyLink} from '@/api/config/friendly-link'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const { proxy } = getCurrentInstance();
const router = useRouter()
function initTableItem() {
// 重置
  operationItem.menuArray[0].handle = () => {
    resetSearch()
  }
// 查询
  operationItem.menuArray[1].handle = () => {
    search()
  }
// 新增
  operationItem.menuArray[2].handle = () => {
    addHandle()
  }
  //详情
  tableItem.btnArray[0].handle = (row) => {
    goDetail(row)
  }
//修改
  tableItem.btnArray[1].handle = (row) => {
    editHandle(row)
  }
//上架
  tableItem.btnArray[2].handle = (row) => {
    publishHandle(1, row)
  }
//下架
  tableItem.btnArray[3].handle = (row) => {
    publishHandle(0, row)
  }
//删除
  tableItem.btnArray[4].handle = (row) => {
    delHandle(row)
  }
}

function initRequest() {
  tableItem.pageInfo.request = {
    'name': '',
    'publishedFlag':null,
    'pageDomain': {
      'pageNum': '1',//分页页面数
      'pageSize': '10',//分页页面数据大小
      'orderByColumn': '',//搜索参数
      'isAsc': '',//搜索参数
      'reasonable': ''//搜索参数
    }
  }
}

function search() {
  tableItem.pageInfo.paging()
}
function resetSearch() {
  initRequest()
  search()
}

function addHandle() {
  router.push({
    path: '/modules/config/friendly-link/add',
    query: { type: 'add' }
  })
}
function goDetail(row) {
  router.push({
    path: '/modules/config/friendly-link/add',
    query: { id: row.id, type: 'view' }
  })
}
function editHandle(row) {
  router.push({
    path: '/modules/config/friendly-link/add',
    query: { id: row.id, type: 'edit' }
  })
}
//上下架
function publishHandle(publishedFlag, row) {
  let dataRequest = {
    id: row.id,
    publishedFlag: publishedFlag
  }
  publishFriendlyLink(dataRequest).then(res => {
    if (res.code === 200) {
      tableItem.pageInfo.paging()
      ElMessage.success('操作成功');
    }
  })
}
//删除
function delHandle(row) {
  proxy.$modal.confirm("确定删除该数据？").then(() => {
    delFriendlyLink(row.id).then(res => {
      if (res.code === 200) {
        tableItem.pageInfo.paging()
        ElMessage.success('删除成功');
      }
    })
  })
}

onMounted(() => {
  initTableItem()
  initRequest()
})
</script>

<style></style>
